/*jslint browser:true */
"use strict";
var controllersModule = angular.module('companySearchControllers', ['companySearchServices']);
var AdminData = AdminData || {};


controllersModule.controller('CompanySeachController', function ($scope, companyAPIService) {
    var subscriber = null;

    $scope.searching = false;
    $scope.query = "";

    $scope.onInputChanged = function () {
        $scope.position = null;
        $scope.esquery = null;
        if (subscriber) {
            var queryData = {
                postion: null,
                query: $scope.query,
                repository: "mongo",
            };
            subscriber(queryData);
        }
    };

    $scope.onESInputChanged = function () {
        $scope.position = null;
        $scope.query = null;
        if (subscriber) {
            var queryData = {
                postion: null,
                query: $scope.esquery,
                repository: "es"
            };
            subscriber(queryData);
        }
    };

    ///should be implemented with switchLatest https://github.com/Reactive-Extensions/RxJS-Examples/blob/master/autocomplete/autocomplete.js
   
    var inputStream = Rx.Observable
        .fromEventPattern(function addHandler(h) {
            subscriber = h;
        })
        .throttle(300)
        .distinctUntilChanged();
    

    inputStream.subscribe(
        function (queryData) {
            $scope.search(queryData);
        }
    );

    $scope.nextPage = function () {
        var queryData = null;
        if ($scope.lastSearch == "mongo") {
            queryData = {
                position: $scope.position,
                query: $scope.query,
                repository : "mongo"
            };
        
        } else {
            queryData = {
                position: $scope.position,
                query: $scope.esquery,
                repository: "es"
            }
        }

        
        $scope.search(queryData);
    };

    $scope.search = function (data) {
        $scope.searching = true;
        $scope.errorMessage = "";
        var lastSearch = "mongo";
        var search = companyAPIService.search;
        if (data.repository == "es") {
            search = companyAPIService.essearch;
            lastSearch = "es";
        }
        search(data)
            .success(function (data) {
                $scope.companies = data.companies;
                $scope.searching = false;
                $scope.position = data.position;
                $scope.lastSearch = lastSearch;
                $scope.hasMore = false;
                if (data.position && (data.position.containsIndex >= 0 || data.position.startsWithIndex >= 0)) {
                    $scope.hasMore = true;
                }
            })
            .error(function (data, status) {
                $scope.companies = [];
                $scope.searching = false;
                $scope.hasMore = false;
                $scope.position = null;
                $scope.errorMessage = "Failed to execute the search operation.";;
            });
    };
 
   
});
